﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.gwxfun
{
    public partial class reply_imgs_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            //取到操作类型
            string _action = DTRequest.GetQueryString("action");
            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.sf_reply_imgs().Exists(this.id))
                {
                    JscriptMsg("回复不存在或已被删除！", "back");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.sf_reply_imgs replyBll = new BLL.sf_reply_imgs();
            Model.sf_reply_imgs model = replyBll.GetModel(_id);
            if (model.matchModel == 1)
            {
                cbIsLock.Checked = true;
            }
            else
            {
                cbIsLock.Checked = false;
            }
            txtImgUrl.Text = model.replyImg;
            imgHeadPic.ImageUrl = model.replyImg;
            txtKeyWords.Text = model.keywords;
            txtSeoDescription.Text = model.replyMsg;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            ChkAdminLevel("reply_imgs_list", DTEnums.ActionEnum.Add.ToString()); //检查权限
            bool result = false;
            Model.sf_reply_imgs model = new Model.sf_reply_imgs();
            BLL.sf_reply_imgs colorBll = new BLL.sf_reply_imgs();
            //商户 公众号 模版等信息
            Model.manager mgr = Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager;
            string busnieseNum = mgr.busnieseNum.ToString();
            string appNum = Session[DTKeys.SESSION_APP_INFO].ToString();

            model.replyImg = txtImgUrl.Text;
            model.keywords = txtKeyWords.Text;
            model.replyMsg = txtSeoDescription.Text.Trim();
            model.busnieseNum = busnieseNum;
            model.appNum = appNum;
            model.addTime = DateTime.Now;
            if (cbIsLock.Checked == true)
            {
                model.matchModel = 1;
            }
            else
            {
                model.matchModel = 0;
            }
            if (colorBll.Add(model) > 0)
            {
                AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加图文回复信息:" + model.keywords); //记录日志
                result = true;
            }
            return result;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            ChkAdminLevel("reply_imgs_list", DTEnums.ActionEnum.Edit.ToString()); //检查权限
            bool result = false;
            BLL.sf_reply_imgs replyBll = new BLL.sf_reply_imgs();
            Model.sf_reply_imgs model = replyBll.GetModel(_id);
            model.replyImg = txtImgUrl.Text;
            model.keywords = txtKeyWords.Text;
            model.replyMsg = txtSeoDescription.Text.Trim();
            if (cbIsLock.Checked == true)
            {
                model.matchModel = 1;
            }
            else
            {
                model.matchModel = 0;
            }
            if (replyBll.Update(model))
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改图文回复信息:" + model.keywords); //记录日志
                result = true;
            }

            return result;
        }
        #endregion


        // 提交保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改图文回复成功！", "reply_imgs_list.aspx");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加图文回复成功！", "reply_imgs_list.aspx");
            }
        }

    }
}